package cn.com.dhc.danlu.util;

import android.annotation.SuppressLint;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import cn.com.hd.mall.web.webservices.entity.BaseResponse;
import cn.com.hd.mall.web.webservices.entity.freedom.entity.coupon.ActivityEntity;
import cn.com.hd.mall.web.webservices.entity.freedom.entity.coupon.CouponEntity;
import cn.com.hd.mall.web.webservices.entity.freedom.response.coupon.AchieveCouponResponse;
import cn.com.hd.mall.web.webservices.entity.freedom.response.coupon.GetCouponListResponse;
import cn.com.hd.mall.web.webservices.entity.freedom.response.coupon.ObtainDealersCouponList;

/**
 * @author: 李一方
 * @time: 2016-10-25 16:00
 * @mail: liyifang@danlu.com
 */
public class MockUtil {
    private final static int mPageSize = 10;

    private static String randomDate (String beginDate, String endDate) {
        try {
            @SuppressLint ("SimpleDateFormat")
            SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd");
            Date start = format.parse (beginDate);// 构造开始日期
            Date end = format.parse (endDate);// 构造结束日期
            // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
            if (start.getTime () >= end.getTime ()) {
                return null;
            }
            long date = randomDate (start.getTime (), end.getTime ());

            return format.format (new Date (date));
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return null;
    }

    private static long randomDate (long begin, long end) {
        long rtn = begin + (long) (Math.random () * (end - begin));
        // 如果返回的是开始时间和结束时间，则递归调用本函数查找随机值
        if (rtn == begin || rtn == end) {
            return randomDate (begin, end);
        }
        return rtn;
    }

    public static BaseResponse mockEntity () {
        return mockAchieveCouponResponse ();
        //        return mockUsedEntity ();
        //        return mockObtainEntity ();
    }

    public static AchieveCouponResponse mockAchieveCouponResponse () {
        AchieveCouponResponse response = new AchieveCouponResponse ();
        //        response.setSuccess (randomNum (3));
        //        response.setCouponAmt (randomNum (1000));
        response.setSuccess ("0");
        response.setCouponAmt ("200");
        response.setCouponId ("C01010546");
        response.setLeftTimes ("3");
        return response;

    }

    private static String randomNum (int limit) {
        Random random = new Random (limit);
        return "" + random.nextInt ();
    }

    public static ObtainDealersCouponList mockObtainEntity () {
        Random random = new Random ();
        ObtainDealersCouponList obtainDealersCoupon = new ObtainDealersCouponList ();
        List<ActivityEntity> datas = new ArrayList<> (mPageSize);
        for (int i = 0; i < mPageSize; i++) {
            ActivityEntity entity = new ActivityEntity ();
            entity.setActivityId ("AAAAAAAAAAAAAAAAAAA");
            int flag = (random.nextInt (3) + 1);
            entity.setActivityStatus ("0" + (flag == 3 ? 4 : flag));
            entity.setCouponAmt (random.nextBoolean () ? "0" : ("" + (random.nextInt (100) * 100 + 100)));
            entity.setAmtUseLimit (random.nextBoolean () ? ("满" + (random.nextInt (100) * 10 + 10) + "可以使用") : ("无启用金额限制"));
            entity.setDealerLimit ("丹露专卖店");
            entity.setChannelLimit ("烟酒专卖店");
            entity.setMerchLimit ("人头马XXOO");
            entity.setPlatformLimit ("web、app");
            entity.setExpireDate (randomDate ("2015-01-01", "2016-10-01"));
            entity.setUseDate (randomDate ("2015-01-01", "2016-10-01"));
            datas.add (entity);
        }
        obtainDealersCoupon.setActivityList (datas);
        return obtainDealersCoupon;
    }

    public static GetCouponListResponse mockUsedEntity () {
        Random random = new Random ();
        GetCouponListResponse getCouponList = new GetCouponListResponse ();
        List<CouponEntity> datas = new ArrayList<> (mPageSize);
        for (int i = 0; i < mPageSize; i++) {
            CouponEntity entity = new CouponEntity ();
            entity.setCouponCode (System.currentTimeMillis () + "016");                         // 优惠券/红包编号
            entity.setOrderNo (System.currentTimeMillis () + "016");                            // 使用订单编号(已使用的经销商优惠券/红包特有)
            entity.setCouponName ("测试用券 - " + (i + 1));                         // 惠券/红包名称
            int flag = (random.nextInt (3) + 1);
            entity.setCouponStatus ("0" + (flag == 3 ? 4 : flag));
            entity.setCouponAmt (random.nextBoolean () ? "0" : ("" + (random.nextInt (100) * 100 + 100)));                          // 优惠券/红包金额
            entity.setTimeLeft ("" + (random.nextInt (5) + 1));                           // 剩余天数
            entity.setExpireDate (randomDate ("2015-01-01", "2016-10-01"));                         //过期日期
            entity.setUseDate (randomDate ("2015-01-01", "2016-10-01"));                            // 使用日期
            entity.setAmtUseLimit (random.nextBoolean () ? ("满" + (random.nextInt (100) * 10 + 10) + "可以使用") : ("无启用金额限制"));                        // 金额使用限制
            entity.setDealerLimit ("丹露专卖店");                        // 经销商使用限制
            entity.setChannelLimit ("烟酒专卖店");                       // 渠道使用限制
            entity.setMerchLimit ("狗日的马化腾");                         // 商品使用限制
            entity.setPlatformLimit ("2016" + (System.currentTimeMillis () + random.nextInt (1000000000)));                      // 平台使用限制
            entity.setMerchId ("016" + (System.currentTimeMillis () + random.nextInt (1000000000)));                            // 商品ID
            datas.add (entity);
        }
        getCouponList.setCouponList (datas);
        return getCouponList;
    }

}
